﻿using agsXMPP;
using agsXMPP.protocol.client;
using agsXMPP.protocol.iq.roster;
using JClient.Contacts;
using ScriptsAPI;

namespace JClient {
	public partial class ConnectionProvider {
		#region XMPP & Temp
		public void loadTemp(string Login, string Server, string Resource, string Status, ShowType PShow, PresenceType PType) {
			Me.Jid = new Jid("") {
				User = Login,
				Server = Server,
				Resource = Resource
			};
			Me.Status = Status;
			Me.ShowType = PShow;
			Me.PresenceType = PType;
		}
		public void loadXMPP(string Login, string Server, string Password, int Port, ShowType PShow, string Status, bool RegisterAccount, bool AutoResolveConnectServer, bool AutoAgents, bool AutoRoster, bool KeepAlive, int KeepAliveInterval, bool UseSSL, bool UseStartTLS, int Priority, string ClientVersion, string Resource) {
			Client.Username = Login;
			Client.Server = Server;
			Client.Password = Password;
			Client.Port = Port;
			Client.Show = PShow;
			Client.Status = Status;
			Client.RegisterAccount = RegisterAccount;
			Client.AutoResolveConnectServer = AutoResolveConnectServer;
			Client.AutoAgents = AutoAgents;
			Client.AutoRoster = AutoRoster;
			Client.KeepAlive = KeepAlive;
			Client.KeepAliveInterval = KeepAliveInterval;
			Client.UseSSL = UseSSL;
			Client.UseStartTLS = UseStartTLS;
			Client.Priority = Priority;
			Client.ClientVersion = ClientVersion;
			Client.Resource = Resource;
			//Zdarzenia
			//if (!DefaultLoaded) {

			// }
		}
		#endregion
		#region Bot
		public void connect( ) {
			/* Ustanowienie połączenia szyfrowanego z serwerem. */
			if (this.XmppConnectionState == XmppConnectionState.Connecting)
				this.disconnect( );
			if (this.XmppConnectionState == XmppConnectionState.Disconnected || this.XmppConnectionState == XmppConnectionState.Connecting)
				Client.Open( );
		}
		public void presenceTransports( ) {
			Presence pres = new Presence(Me.ShowType, Me.Status);
			pres.Type = Me.PresenceType;
			Client.Send(pres);
			if (Services != null) {
				foreach (RosterContact service in Services) {
					pres.To = service.Jid;
					Client.Send(pres);
				}
			}
		}
		public void setStatus(int status) {
			Presence p = JFunctions.getStatusPre(status);
			Me.PresenceType = p.Type;
			Me.ShowType = p.Show;
			presenceTransports( );
		}
		public void setDescription(string desc) {
			Me.Status = desc;
			presenceTransports( );
		}
		public void sendXml(string xml) {
			this.Client.Send(xml);
		}
		public void sendXml(agsXMPP.Xml.Dom.Element e) {
			this.Client.Send(e);
		}
		public void disconnect( ) {
			/* Zatrzymanie pracy bot'a i zamknięcie. */
			if (Client.XmppConnectionState != XmppConnectionState.Disconnected) {
				Client.Close( );
			}
		}
		#endregion
		#region User
		public void authorize(Jid cJid, string msg, int type) {
			switch (type) {
				case 0:
					// Anulowanie
					Client.PresenceManager.RefuseSubscriptionRequest(cJid);
					break;
				case 1:
					// Autoryzuj
					Client.PresenceManager.ApproveSubscriptionRequest(cJid);
					break;
				case 2:
					// Prośba o anulowanie subskrybcji
					Client.PresenceManager.Unsubscribe(cJid);
					break;
				case 3:
					// Prośba o subskrypcję + dodanie kontaktu
					if (msg != null)
						Client.PresenceManager.Subscribe(cJid, msg);
					else
						Client.PresenceManager.Subscribe(cJid);
					break;
			}
		}
		public void addRosterItem(Jid RJid, string RNickname, string RGroup) {
			if (RJid != null && RJid.ToString( ) != "") {
				if (RNickname == null || RNickname == "")
					RNickname = null;
				if (RGroup == null || RGroup == "")
					RGroup = "";
				Client.RosterManager.AddRosterItem(RJid, RNickname, RGroup);
			}
		}
		public void editRosterItem(Jid RJid, string RNickname, string RGroup) {
			if (RJid != null && RJid.ToString( ) != "") {
				if (RNickname == null || RNickname == "")
					RNickname = null;
				if (RGroup == null || RGroup == "")
					RGroup = "";
				Client.RosterManager.UpdateRosterItem(RJid, RNickname, RGroup);
			}
		}
		public void delRosterItem(Jid RJid) {
			if (RJid != null)
				Client.RosterManager.RemoveRosterItem(RJid);
		}
		#endregion
		#region Packets
		public void send(Jid user, string text, agsXMPP.protocol.client.Message reply_to) {
			/* Wysyłanie wiadomosci do określonego użytkownika. */
			agsXMPP.protocol.client.Message mess;
			if (user != null && text != null) {
				mess = new agsXMPP.protocol.client.Message(user, text);
				if (reply_to != null) {
					mess.Thread = reply_to.Thread;
					mess.Type = reply_to.Type;
				}
				mess.Body = text;
				mess.To = user;
				Client.Send(mess);
			}
		}
		#endregion
	}
}